﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Grizzy_X
{
    public partial class NewMapForm : Form
    {
        public NewMapForm()
        {
            InitializeComponent();
            IfNewDB.Checked = true;
            Random ran = new Random();
            MapName.Text = Convert.ToString(ran.Next(9999999));
            Unit.SelectedIndex = 0;
        }
        public string MapPath;
        public int MapWidth;
        public int MapHeight;
        public string MapDes;
        //点击确定，新建地图
        private void button2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Path.Text))
            {
               /*
                Controller.check = false;
                MessageBox.Show("警告：请选择地图文件路径！");
                */
            }
            else
            {
                Controller.MyMap = new CMap();
                Controller.mapname = MapName.Text;
                Controller.check = true;
                MapPath = Path.Text;
                Controller.MapPath = Path.Text;
                MapWidth = (int)Convert.ToDouble(Width.Text) * 96;
                MapHeight = (int)Convert.ToDouble(Height.Text) * 96;
                if (Unit.Text == "厘米")
                {
                    MapWidth = (int)(Convert.ToDouble(Width.Text) / 2.54 * 96);
                    MapHeight = (int)(Convert.ToDouble(Height.Text) / 2.54 * 96);
                }
                MapDes = Des.Text;
                Persistance.CreateDirectory();//新建文件夹
                if (IfNewDB.Checked == true)
                {
                    try
                    {
                        Persistance.CreateDatabase();
                        //MessageBox.Show("创建数据库成功，默认与地图文件在同一文件夹下。");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                        MessageBox.Show("创建数据库失败，请检查链接或地图文件路径！");
                        Controller.DBExists = false;
                    }
                    Controller.DBExists = true;
                }
                else
                {
                    Controller.DBExists = false;

                }    
                    //记录原始的数据，再记录坐标？？？ 

                    Controller.MyMap.New(MapName.Text, Convert.ToDouble(Height.Text), Convert.ToDouble(Width.Text), MapPath, MapDes, Unit.Text);
                    this.Close();
                
            }
        }

        private void NewMapForm_Activated(object sender, EventArgs e)
        {
            //IfNewDB.Checked = true;
        }

        private void Browse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog MapBrowse = new FolderBrowserDialog();

            if (MapBrowse.ShowDialog() == DialogResult.OK)
            {
                Path.Text = MapBrowse.SelectedPath;
            }
        }

        private void Unit_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if()
        }
    }
}
